package com.example.fastshareapp;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.alljoyn.bus.BusObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.MediaStore.Images;
import android.util.Base64;
import android.util.Log;


enum DATASENDINGSTATUS {
	NONE, SENDING, DONE, ERROR
}

public class Utilities {

	public synchronized static Bitmap getImageLocal(String filePath) {
		return getImageLocal(filePath, BitmapUtil.REQUEST_WIDTH,
				BitmapUtil.REQUEST_HEIGHT);
	}

	public synchronized static Bitmap getImageLocal(String filePath,
			int reqWidth, int reqHeight) {
		if (reqWidth == -1 || reqHeight == -1) { // no subsample and no
			return BitmapFactory.decodeFile(filePath);
		} else {
			// First decode with inJustDecodeBounds=true to check dimensions
			final BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(filePath, options);

			// Calculate inSampleSize
			options.inSampleSize = BitmapUtil.calculateInSampleSize(options,
					reqWidth, reqHeight);

			// Decode bitmap with inSampleSize set
			options.inJustDecodeBounds = false;

			return BitmapFactory.decodeFile(filePath, options);
		}
	}

	public synchronized static Bitmap getImageLocal(String filePath, int percent) {
		if (percent > 90) { // no subsample and no
			return BitmapFactory.decodeFile(filePath);

		} else {
			// First decode with inJustDecodeBounds=true to check dimensions
			final BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(filePath, options);
			// Calculate inSampleSize
			options.inSampleSize = BitmapUtil.calculateInSampleSize(options,
					percent);
			// Decode bitmap with inSampleSize set
			options.inJustDecodeBounds = false;
			return BitmapFactory.decodeFile(filePath, options);
		}
	}

	public synchronized static String EncodeFromString(String data) {
		return Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
	}

	public synchronized static String EncodeFromImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.JPEG, 50, stream);
		byte[] byteArray = stream.toByteArray();
		return Base64.encodeToString(byteArray, Base64.DEFAULT);
	}

	public synchronized static String DecodeToString(String data)
			throws UnsupportedEncodingException {
		return new String(Base64.decode(data, Base64.DEFAULT), "UTF-8");
	}

	public synchronized static Bitmap DecodeToImage(String data) {
		byte[] image = Base64.decode(data, Base64.DEFAULT);
		return BitmapFactory.decodeByteArray(image, 0, image.length);
	}
	
	public synchronized static Bitmap createBitmapFromList(List<Image> listImage)
	{
		int width = 0;
		int height = 0;
		for(Image img: listImage)
		{
			width = img.getWidth();
			height+=img.getHeight();
		}
		Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas combine = new Canvas(result);
		int posX=0;
		int posY = 0;
		for(Image img: listImage)
		{
			combine.drawBitmap(img.makeImage(), posX, posY, null);
			posY += img.getHeight();
		}
		return result;
	}

	public synchronized static List<Image> SplitBitmap(String imageName,Bitmap image) {
		int imageWid = image.getWidth();
		int imageHei = image.getHeight();
		int width = 100;
		int height = 100;
		List<Image> result = new ArrayList<Image>();
		int posX = 0;
		int posY = 0;
		int row = 0;
		int col = 0;
		if(imageHei % height!=0)
		{
			row = imageHei / height +1;
		}
		else
		{
			row = imageHei / height;
		}
		if(imageWid % width!=0)
		{
			col = imageWid / width +1;
		}
		else
		{
			col = imageWid / width;
		}
		
		Log.e("Image", "Total row " + (row));
		Log.e("Image", "Total column " + (col));
		
		for (int y = 0; y < row; y++) {
			Image img = new Image(y,imageName);
			for (int x = 0; x < col; x++) {

				if (imageHei - posY < height && imageWid - posX < width && ( imageWid - posX != 0 && imageHei - posY !=0)) {
					Log.e("Image", "image: " + posX + "-" + posY + "-"
							+ (imageWid - posX) + "-" + (imageHei - posY));
					Bitmap _pieceadd = Bitmap.createBitmap(image, posX, posY,
							imageWid - posX, imageHei - posY);
					img.AddImage(_pieceadd);
				} else {
					if (imageHei - posY < height && imageHei - posY > 0 && imageWid - posX != 0) {
						Log.e("Image", "image: " + posX + "-" + posY + "-"
								+ (width - 1) + "-" + (imageHei - posY));
						Bitmap _pieceadd = Bitmap.createBitmap(image, posX,
								posY, width - 1, imageHei - posY);
						img.AddImage(_pieceadd);
					} else {
						if (imageWid - posX < width && imageWid - posX > 0 && imageHei - posY !=0 ) {
							Log.e("Image", "image: " + posX + "-" + posY + "-"
									+ (imageWid - posX) + "-" + (height - 1));
							Bitmap _pieceadd = Bitmap.createBitmap(image, posX,
									posY, imageWid - posX, height - 1);
							img.AddImage(_pieceadd);
						} else {
							if (imageWid - posX != 0 && imageHei - posY !=0) {
								Log.e("Image", "image: " + posX + "-" + posY
										+ "-" + (width - 1) + "-"
										+ (height - 1));
								Bitmap _piece = Bitmap.createBitmap(image,
										posX, posY, width - 1, height - 1);
								img.AddImage(_piece);
							}
						}
					}
				}
				posX += width;
			}
			result.add(img);
			posY += height;
			posX = 0;
		}
		image.recycle();
		return result;
	}

	static class BitmapUtil {
		public static final int REQUEST_WIDTH = 100;
		public static final int REQUEST_HEIGHT = 100;

		public synchronized static int calculateInSampleSize(
				BitmapFactory.Options options) {
			return calculateInSampleSize(options, REQUEST_WIDTH, REQUEST_HEIGHT);
		}

		public synchronized static int calculateInSampleSize(
				BitmapFactory.Options options, int reqWidth, int reqHeight) {

			// Raw height and width of image
			final int height = options.outHeight;
			final int width = options.outWidth;
			int inSampleSize = 1;

			if (height > reqHeight || width > reqWidth) {
				// Calculate ratios of height and width to requested height and
				// width
				final int heightRatio = Math.round((float) height
						/ (float) reqHeight);
				final int widthRatio = Math.round((float) width
						/ (float) reqWidth);

				// Choose the smallest ratio as inSampleSize value, this will
				// guarantee
				// a final image with both dimensions larger than or equal to
				// the
				// requested height and width.
				inSampleSize = heightRatio < widthRatio ? heightRatio
						: widthRatio;
			}
			return inSampleSize;
		}

		public synchronized static int calculateInSampleSize(
				BitmapFactory.Options options, int percent) {

			// Raw height and width of image
			final int height = options.outHeight;
			final int width = options.outWidth;
			int inSampleSize = 1;

			if (percent < 100 && (height> 800 || width > 1000)) {
				// Calculate ratios of height and width to requested height and
				// width
				float reqHeight = height * percent / 100;
				float reqWidth = width * percent / 100;
				final int heightRatio = Math.round((float) height
						/ (float) reqHeight);
				final int widthRatio = Math.round((float) width
						/ (float) reqWidth);

				// Choose the smallest ratio as inSampleSize value, this will
				// guarantee
				// a final image with both dimensions larger than or equal to
				// the
				// requested height and width.
				inSampleSize = heightRatio < widthRatio ? heightRatio
						: widthRatio;
			}

			return inSampleSize;
		}
	}
}
